package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;

import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart save(Cart cartParam) {
        //1. 查询当前菜品/套餐在数据表中是否存在(当前登录用户user_id)
        //select * from cart where user_id = 登录用户id and dish_id= 传入的菜品id
        //select * from cart where user_id = 登录用户id and setmeal_id = 传入的套餐id
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId())//user_id = 登录用户的id
        .eq(cartParam.getDishId() != null,Cart::getDishId,cartParam.getDishId())//and dish_id = 传入的菜品id
        .eq(cartParam.getSetmealId() !=null,Cart::getSetmealId,cartParam.getSetmealId());//and setmeal_id = 传入的套餐id
        Cart cart = cartMapper.selectOne(wrapper);


        if (cart != null){
            //2 查到了当前菜品/套餐在数据库的记录，只需要将number+1更新
            cart.setNumber(cart.getNumber() +1);
            cartMapper.updateById(cart);

            return cart;

        }else {

            //3 没有查询到当前菜品/套餐的记录，更新记录
                //设置为登录用户的id
            cartParam.setUserId(UserHolder.get().getId());
            cartParam.setNumber(1);//设置为1
            cartParam.setCreateTime(new Date());//保存时间
            cartMapper.insert(cartParam);
            return cartParam;

        }


    }
    //回显我的购物车
    @Override
    public List<Cart> findList() {
        //设置查询条件
        //select * from 'shopping_cart' where user_id = '1243431112132....' order by create_time ASC
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId())//user_id = ?
        .orderByAsc(Cart::getCreateTime);//order by create_time ASC

        //执行查询
        return cartMapper.selectList(wrapper);
    }
    //减少购物车中的数量
    @Override
    public Cart update(Cart cart) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null,Cart::getDishId,cart.getDishId()).eq(Cart::getUserId,UserHolder.get().getId()).eq(cart.getSetmealId() != null,Cart::getSetmealId,cart.getSetmealId());
        Cart cart2 = cartMapper.selectOne(wrapper);

        Cart cart1 = new Cart();
        cart1.setNumber(cart2.getNumber() - 1);

        LambdaQueryWrapper<Cart> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(cart.getDishId() != null,Cart::getDishId,cart.getDishId()).eq(Cart::getNumber,0).eq(cart.getSetmealId() != null,Cart::getSetmealId,cart.getSetmealId());

        cartMapper.update(cart1,wrapper);

        cartMapper.delete(wrapper1);

        return cart1;

    }

    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }
}
